package com.gisuni.sddc.platformManager.controller;

import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import com.gisuni.sddc.platformManager.model.PageInfo;
import com.gisuni.sddc.platformManager.model.Response;
import com.gisuni.sddc.platformManager.model.RoleInfo;
import com.gisuni.sddc.platformManager.service.RoleService;
import com.gisuni.sddc.platformManager.service.UserService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;


@IocBean(singleton = false)
@At("/client/backgroundManagement/roleController")
public class RoleController   {

	private Log log = Logs.getLog(UserController.class);
 
	
	@Inject
    private RoleService roleService;

 
    
 
	@At("/dataGrid")
	@GET
	@POST
	@Ok("raw:json") 
    public Object dataGrid(@Param("name") String roleName,@Param("page") int page, @Param("rows") int rows,HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			int from=(page-1)*rows;
//			res = roleService.queryBaseRoleAll(from,from+rows+1,roleName);
			res = roleService.queryBaseRoleAll(page,rows,roleName);
			r = Response.toJSONObject(res);
//			ObjectMapper mapper = new ObjectMapper();
//			r = mapper.readValue(o.toString(), Map.class);
//			System.out.println(r.toString());
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
    }

 
	@At("/tree")
	@GET
	@POST
	@Ok("json") 
    public Object tree() {
        return roleService.findTree();
    }

    
  

 
	@At("/add")
	@GET
	@POST
	@Ok("json")
    public Object add(HttpServletRequest request) {
       
		Response res = new Response();
		res.setSuccess(false);
		try {
			res =   roleService.addRole(request);//返回格式:{}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return res;
    }
    
	@At("/edit")
	@GET
	@POST
	@Ok("json")
    public Object edit(HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			res = roleService.updateRole(request); 
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return res;
    }
	

	@At("/delete")
	@GET
	@POST
	@Ok("json")
    public Object delete(String id) {
		Response res = new Response();
		res.setSuccess(false);
		try {
			res =  roleService.deleteRoleById(id);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return res;
    }

    
	@At("/editPage")
	@GET
	@POST
	@Ok("jsp:/client/backgroundManagement/views/admin/roleEdit.jsp")
    public String editPage( HttpServletRequest request, String id) {
        RoleInfo role = roleService.findRoleById(id);
        request.setAttribute("role", role);
        request.setAttribute("roleArea", role.getRoleArea());
        return null;
    }

 


    
 
   	@At("/findResourceIdListByRoleId")
   	@GET
   	@POST
   	@Ok("json")
    public Object findResourceByRoleId(Long id) {
//        List<Long> resources = roleService.findResourceIdListByRoleId(id);
//        return renderSuccess(resources);
        return null;
    }

   	@At("/grantModule")
   	@GET
   	@POST
   	@Ok("json")
    public Object grantModule(String roleId, String moduleIds) {
		Response res = new Response();
		res.setSuccess(false);
		try{
			res= roleService.grantModule(roleId, moduleIds);
			 
		}catch(Exception e){
			log.error(e.getMessage()+e.getCause());
			res.setMsg("操作失败");
		}
        
        return res;
    }
 
   	@At("/grantResource")
   	@GET
   	@POST
   	@Ok("json")
    public Object grantResource(String roleId, String resourceIds) {
		Response res = new Response();
		res.setSuccess(false);
		try{
			res= roleService.grantResource(roleId, resourceIds);
			 
		}catch(Exception e){
			log.error(e.getMessage()+e.getCause());
			res.setMsg("操作失败");
		}
        
        return res;
    }
   	
   	/**
   	 * 得到角色关联的模块ID与资源ID
   	 * @param id
   	 * @return
   	 */
   	@At("/getPrivilege")
   	@GET
   	@POST
   	@Ok("json")
    public Object getPrivilegeByRoleId(String id) {
   		return roleService.getPrivilegeByRoleId(id);
    }
   	
   	
   	
   	

}
